package com.baozun.houji.order.util.order

import android.view.View
import android.view.ViewGroup
import com.baozhun.mall.common.base.BaseApplication
import com.baozhun.mall.common.listener.ViewOnClickListener
import com.baozhun.mall.common.util.DialogUtil
import com.baozun.houji.order.R
import com.baozun.houji.order.activity.ApplyRefundActivity
import com.baozun.houji.order.databinding.ItemOrderActionLayoutBinding
import com.baozhun.mall.common.model.enums.OrderStatus
import com.baozhun.mall.common.model.enums.RefundStatus
import com.baozun.houji.order.util.order.enums.OrderActionType
import com.baozun.houji.order.util.order.enums.OrderSource
import com.baozun.houji.order.viewmodel.OrderListViewModel
import com.baozun.houji.order.widget.ChoosePayWayDialog
import com.blankj.utilcode.util.ActivityUtils

/**
 * @Title：订单事件管理类
 * @Description：
 * 列表页和详情页都是
 * 待付款状态-取消订单btn+立即缴费btn
 * 待发货状态-申请退款btn
 * 待收货状态-申请退换btn+完成订单btn
 * 已完成状态-再次加购btn
 * 已取消状态-再次加购btn
 * @Author：xuefeng.zhu
 * @Since：2022/12/3
 * @Version：1.0.0
 */
class OrderActionManager : ViewOnClickListener {

    private lateinit var mBinding: ItemOrderActionLayoutBinding
    private var mOrderStatus: Int? = 0
    private var mRefundStatus: Int? = 0
    private var mOrderSource = OrderSource.ORLDER_lIST
    private var mAmount: String = ""
    private lateinit var mOrderId: String
    private var mRefundNo: String? = null
    private var mOrderActionFinishListener: OrderActionFinishListener? = null
    private val mRequestViewModel: OrderListViewModel by lazy { OrderListViewModel() }
    private var mLeftBtAction: OrderActionType? = null
    private var mRightBtAction: OrderActionType? = null

    companion object {

        /**
         * 订单列表由于复用原因，必须每个item用一个Instance,来源由字段[isOrderDetail]区分
         */
        fun getInstance(
            orderId: String, refundNo: String?, //售后单号
            orderStatus: Int?, refundStatus: Int?,
            orderSource: OrderSource = OrderSource.ORLDER_lIST,
            amount: String,
            actionResultListener: OrderActionFinishListener?
        ) = if (orderSource == OrderSource.ORLDER_DETAIL || orderSource == OrderSource.REFUND_DETAIL) {
            InstanceHelper.manager
        } else {
            OrderActionManager()
        }.apply {
            mOrderStatus = orderStatus
            mRefundStatus = refundStatus
            mOrderSource = orderSource
            mOrderId = orderId
            mOrderId = orderId
            mRefundNo = refundNo
            mAmount = amount
            mOrderActionFinishListener = actionResultListener
        }
    }

    object InstanceHelper {
        val manager = OrderActionManager()
    }

    /**
     * 添加事件Bt
     */
    fun addActionBt(mainView: ViewGroup?) {
        mainView?.removeAllViews()
        mBinding = ItemOrderActionLayoutBinding.inflate(ActivityUtils.getTopActivity().layoutInflater!!)
        var btActionPair = getBtAction()
        mBinding.showLeftBt = btActionPair.first != null
        mBinding.showRightBt = btActionPair.second != null
        mBinding.leftActionBt.text = btActionPair.first?.actionName
        mBinding.rightActionBt.text = btActionPair.second?.actionName
        mBinding.listener = this
        mBinding.executePendingBindings()
        if (btActionPair.first != null || btActionPair.second != null) {
            mainView?.visibility = View.VISIBLE
            mainView?.addView(mBinding?.root)
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.left_action_bt -> {
                handeOrderAction(mLeftBtAction)
            }

            R.id.right_action_bt -> {
                handeOrderAction(mRightBtAction)
            }
        }
    }

    /**
     * 处理订单操作事件
     */
    private fun handeOrderAction(orderActionType: OrderActionType?) {
        when (orderActionType) {
            OrderActionType.CANCEL_ORDER -> DialogUtil.showConfirmDialog(
                ActivityUtils.getTopActivity(), ActivityUtils.getTopActivity().getString(com.baozhun.mall.res.R.string.confirm_cancel_order)
            ) {
                mRequestViewModel.cancelOrder(mOrderId) {
                    mOrderActionFinishListener?.reload(OrderActionType.CANCEL_ORDER)
                }
            }

            OrderActionType.IMME_PAY -> {
                showPayWayDialog()
            }

            OrderActionType.LOOK_LOGISTICS -> {
                mOrderActionFinishListener?.showLogisticsInfo()
            }

            OrderActionType.DELETE_ORDER -> {
                mRequestViewModel.deleteOrder(mOrderId) { mOrderActionFinishListener?.reload(OrderActionType.DELETE_ORDER) }
            }

            OrderActionType.WITHDRAW_APPLY -> {
                mRequestViewModel.cancelRefund(mRefundNo.toString()) { mOrderActionFinishListener?.reload(OrderActionType.WITHDRAW_APPLY) }
            }

            OrderActionType.APPLY_REFUND -> {
                ApplyRefundActivity.start(ActivityUtils.getTopActivity(), mOrderId, mOrderStatus == OrderStatus.WAIT_SEND.status)
            }

            OrderActionType.CONFIRM_RECEIPT -> {
                DialogUtil.showConfirmDialog(ActivityUtils.getTopActivity(), BaseApplication.mInstance.getString(com.baozhun.mall.res.R.string.confirm_order_hint)) {
                    mRequestViewModel.finishOrder(mOrderId) { mOrderActionFinishListener?.reload(OrderActionType.WITHDRAW_APPLY) }
                }
            }

            OrderActionType.LOOK_DETAIL -> {
                if (mOrderSource == OrderSource.REFUND_DETAIL || mOrderSource == OrderSource.REFUND_lIST) mRefundNo?.let { mOrderActionFinishListener?.startRefundDetail(it) }
                else mOrderActionFinishListener?.startOrderDetail(mOrderId)
            }

            else -> {}
        }
    }

    /**
     * 显示选择支付方式dialog
     */
    private fun showPayWayDialog() {
        ChoosePayWayDialog(ActivityUtils.getTopActivity(), mOrderId, mAmount) {
            mOrderActionFinishListener?.reload(OrderActionType.IMME_PAY)
        }.show()
    }

    private fun getBtAction(): Pair<OrderActionType?, OrderActionType?> {
        mLeftBtAction = null
        mRightBtAction = null
        if (mOrderSource == OrderSource.REFUND_DETAIL || mOrderSource == OrderSource.REFUND_lIST) {
            mLeftBtAction = if (mOrderSource == OrderSource.REFUND_lIST) OrderActionType.LOOK_DETAIL else null
            mRightBtAction = if (mRefundStatus == RefundStatus.WAIT_AUDIT.status || mRefundStatus == RefundStatus.WAIT_RETURN_GOODS.status) OrderActionType.WITHDRAW_APPLY else null
            return Pair(mLeftBtAction, mRightBtAction)
        }
        when (mOrderStatus) {
            OrderStatus.WAIT_DELIVERY.status -> {
                mLeftBtAction = OrderActionType.CANCEL_ORDER
                mRightBtAction = OrderActionType.IMME_PAY
            }

            OrderStatus.WAIT_SEND.status -> {
                if (mOrderSource == OrderSource.ORLDER_lIST) mLeftBtAction = OrderActionType.LOOK_DETAIL
                mRightBtAction = null
            }

            OrderStatus.WAIT_RECEIVE.status -> {
                mLeftBtAction = OrderActionType.LOOK_LOGISTICS
                mRightBtAction = OrderActionType.CONFIRM_RECEIPT
            }

            OrderStatus.AFTER_SALE.status -> {
                if (mOrderSource == OrderSource.ORLDER_lIST) mLeftBtAction = OrderActionType.LOOK_DETAIL
                mRightBtAction = null
            }

            OrderStatus.CANCEL.status -> {
                mLeftBtAction = OrderActionType.DELETE_ORDER
                mRightBtAction = null
            }

            OrderStatus.FINISH.status, OrderStatus.CLOSE.status -> {
                mLeftBtAction = if (mOrderSource == OrderSource.ORLDER_DETAIL) {
                    OrderActionType.LOOK_LOGISTICS
                } else {
                    OrderActionType.LOOK_DETAIL
                }
                mRightBtAction = null
            }
        }
        return Pair(mLeftBtAction, mRightBtAction)
    }
}